import random

from DrissionPage import ChromiumPage
import time
import mysql.connector
from DrissionPage._configs.chromium_options import ChromiumOptions

db = mysql.connector.connect(
    host="localhost",  # MySQL服务器地址
    user="root",   # 用户名
    password="123456",  # 密码
    database="bishe"  # 数据库名称
)
# co = ChromiumOptions()
# co.set_argument('--disable-blink-features=AutomationControlled')  # 禁用自动化检测
# co.block_patterns = ['*rac.js*']
dp = ChromiumPage()

try:
    with db.cursor() as cursor:
        # 查询所有href
        sql = "SELECT href FROM taobaodiannaoshuju WHERE param_info IS NULL"
        cursor.execute(sql)
        results = cursor.fetchall()
        results3 = "商品已经下架"
        for row in results:
            href = row[0]
            print("链接是：" + href)
            dp.get(href)
            # dp.wait.load_start()  # 等待页面开始加载
            wait = random.uniform(1, 2)
            time.sleep(wait)  # 额外等待1秒，确保页面稳定
            # dp.run_js('window.stop();')
            # print("已经执行了")
            # lis1 = dp.eles('css:.p-parameter')
            lis2 = dp.eles('css:.Oo3vRXl7BS--tabDetailItem--_60d465f[data-tabindex="1"]')
            # result1 = ', '.join([item.text for item in lis1]).replace('更多参数>>', '')
            result2 = ', '.join([item.text for item in lis2]).replace(', 图文详情, , ','')
            # print("lis1是："+''.join(lis1))
            # print("lis1是：" + result1)
            print("lis2是：" + result2)
            param_info = ''
            if result2:
                param_info += result2
            # if result2:
            #     if param_info:
            #         param_info += ' | ' + result2
            #     else:
            #         param_info = result2
            if not param_info:
                param_info = results3
            update_sql = "UPDATE taobaodiannaoshuju SET param_info = %s WHERE href = %s"
            cursor.execute(update_sql, (param_info, href))
            db.commit()
finally:
    db.close()
